package com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeMain.persenter;

import android.content.Context;
import android.content.Intent;
import android.icu.text.SimpleDateFormat;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.example.anew.journey.mynewjourney.R;
import com.example.anew.journey.mynewjourney.base.MyApplication;
import com.example.anew.journey.mynewjourney.config.RBConstants;
import com.example.anew.journey.mynewjourney.customview.MyRecyclerView;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeMain.adpater.MyLeftAdapter;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeMain.adpater.MyMainDevicesHotAdapter;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeMain.bean.MainDateBean;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeMain.bean.MainListBean;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeMain.model.FirstChangeMainModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeMain.model.ISFirstChangeMainModel;
import com.example.anew.journey.mynewjourney.firstchangelogic.FirstChangeMain.view.ISFirstChangeMainView;
import com.example.anew.journey.mynewjourney.firstchangelogic.frstChangeSecondTimeCardiacDetection.FrstChangeSecondTimeCardiacDetectionActivity;
import com.example.anew.journey.mynewjourney.inteface.OnRecyclerViewItemClickListener;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.List;

import cn.jesse.nativelogger.NLogger;
import okhttp3.Call;
import okhttp3.Response;
import zhy.com.highlight.HighLight;
import zhy.com.highlight.position.OnLeftPosCallback;
import zhy.com.highlight.shape.CircleLightShape;

/**
 * 杭州惟君医疗科技有限公司14
 * Created by 20433 on 2018/3/14
 */

public class FirstChangeMainPersenter {
    private ISFirstChangeMainView mISFirstChangeMainView;
    private MyMainDevicesHotAdapter myMainDevicesHotAdapter;
    private ISFirstChangeMainModel mISFirstChangeMainModel;
    private MyLeftAdapter adapter;
    private String user_id;
    private String token;
    private MyRecyclerView mactivityFirstChangeMainHomePageMyRecyclerview;
    private final SimpleDateFormat formatter;


    public FirstChangeMainPersenter(ISFirstChangeMainView isSFirstChangeMainView) {
        this.mISFirstChangeMainView = isSFirstChangeMainView;
        mISFirstChangeMainModel = new FirstChangeMainModel();
        formatter = new SimpleDateFormat("yyyy-MM-dd");
    }

    // 主页列表
    public void HomepageEquipmentList(MyRecyclerView activityFirstChangeMainHomePageMyRecyclerview, Context mcontxt) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mcontxt);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mactivityFirstChangeMainHomePageMyRecyclerview = activityFirstChangeMainHomePageMyRecyclerview;
        activityFirstChangeMainHomePageMyRecyclerview.setLayoutManager(layoutManager);
        activityFirstChangeMainHomePageMyRecyclerview.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        activityFirstChangeMainHomePageMyRecyclerview.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        activityFirstChangeMainHomePageMyRecyclerview.setArrowImageView(R.mipmap.iconfont_downgrey);
        activityFirstChangeMainHomePageMyRecyclerview.setPullRefreshEnabled(false);  //TODO 禁止下拉刷新完毕
        activityFirstChangeMainHomePageMyRecyclerview.getDefaultFootView().setNoMoreHint("就这么多了");
        // TODO 传入数据
        myMainDevicesHotAdapter = new MyMainDevicesHotAdapter(mcontxt);
        activityFirstChangeMainHomePageMyRecyclerview.setAdapter(myMainDevicesHotAdapter);
        activityFirstChangeMainHomePageMyRecyclerview.refresh();
       loadMyDevices(mcontxt);
    }

    //侧边栏选项
    public void SidebarOptions(final RecyclerView activityFirstChangeMainLeftPopupLeftRecyclerview, Context mcontext) {
        LinearLayoutManager layoutmanager = new LinearLayoutManager(mcontext);
        layoutmanager.setOrientation(LinearLayoutManager.VERTICAL);
        activityFirstChangeMainLeftPopupLeftRecyclerview.setLayoutManager(layoutmanager);
        //设置Adapter
        adapter = new MyLeftAdapter();
        activityFirstChangeMainLeftPopupLeftRecyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view) {
                int postion = activityFirstChangeMainLeftPopupLeftRecyclerview.getChildAdapterPosition(view);
            }

            @Override
            public void onItemFirstButtonClick(View v, int position) {
                switch (position) {
                    case 0:
                        mISFirstChangeMainView.toDevicesActivity();
                        break;
                    case 1:
                        mISFirstChangeMainView.toSettingActivity();
                        break;
                }
            }
        });

    }

    // 点击 添加设备按钮
    public void onClickFAB() {
        mISFirstChangeMainView.toAddDevicsActivity();
    }

    // 点击 搜索按钮
    public void onClickSearch() {
        mISFirstChangeMainView.toSearchActivity();
    }

    // 引导
    public void guidePager(FloatingActionButton activityFirstChangeMainHomePageFbAdd, Context mcontxt) {
        int mask = mISFirstChangeMainModel.takeMask(mcontxt);
        NLogger.e("TAG", "guidePager==mask====" + mask);
        if(mask != 1){
            showNextKnownTipView(activityFirstChangeMainHomePageFbAdd, mcontxt);
        }
       // showNextKnownTipView(activityFirstChangeMainHomePageFbAdd, mcontxt);
    }

    public void showNextKnownTipView(View view, final Context mcontxt) {
        HighLight mHightLight = new HighLight(mcontxt)//
                .intercept(true)//拦截属性默认为true 使下方ClickCallback生效
                .autoRemove(true)
                .setClickCallback(new HighLight.OnClickCallback() {
                    @Override
                    public void onClick() {
                       // Toast.makeText(mcontxt, "clicked and remove HightLight view by yourself", Toast.LENGTH_SHORT).show();
                        mISFirstChangeMainModel.saveMask(mcontxt,1);

                    }
                })
                .addHighLight(R.id.activity_first_change_main_home_page_fb_add, R.layout.custom_tipview, new OnLeftPosCallback(0), new CircleLightShape());
        mHightLight.show();

    }

    public void onClickPersonalInformationPage() {
        mISFirstChangeMainView.toPersonalInformationPage();
    }

    //初始化侧边栏
    public void initializeSidebar(Context mcontxt) {
        MainDateBean bean = mISFirstChangeMainModel.TakeData(mcontxt);
        user_id = bean.getId();
        token = bean.getToken();
        mISFirstChangeMainView.setName(bean.getCus_name());
        mISFirstChangeMainView.setHeadPortrait(bean.getCus_logo());
    }

    //页面可见的时候去访问存储数据 设置头像和昵称
    public void startViewSidebar(Context mcontxt) {
        int versionCode = mISFirstChangeMainModel.getVersionCode(mcontxt);
        //NLogger.e("TAG", "versionCode======" + versionCode);
        String my_use_name = mISFirstChangeMainModel.TakeNiName(mcontxt);
        String my_use_logo = mISFirstChangeMainModel.TakeHeadPortrait(mcontxt);
        if (TextUtils.isEmpty(my_use_name)) return;
        if (TextUtils.isEmpty(my_use_logo)) return;

        mISFirstChangeMainView.setName(my_use_name);
        mISFirstChangeMainView.setHeadPortrait(my_use_logo);
    }

    public void loadMyDevices(final Context mcontxt) {
        String url = RBConstants.SERVER_URL + RBConstants.MY_DEVICES;

        OkHttpUtils
                .post()
                .url(url)
                .addParams("cus_id", user_id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetworkResponseDate = response.body().string();
                        Gson gson = new Gson();
                        NLogger.e("TAG", "===main==22=======NetworkResponseDate=================" + NetworkResponseDate);
                        final MainListBean mainListBean = gson.fromJson(NetworkResponseDate, MainListBean.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if (mainListBean.errorCode == 200) {
                                    final List<MainListBean.DataBean> beanList = mainListBean.data;
                                    myMainDevicesHotAdapter.clearItem();
                                    if (beanList != null) {
                                        myMainDevicesHotAdapter.AddItem(beanList);
                                        myMainDevicesHotAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
                                            @Override
                                            public void onItemClick(View view) {
                                               int postion =  mactivityFirstChangeMainHomePageMyRecyclerview.getChildAdapterPosition(view) -1;
//                                               if(formatter.format(new Date()).equals(mISFirstChangeMainModel.TakeISFirstDate(mcontxt).datestr)){
//                                                   Intent intent = new Intent(mcontxt, FrstChangeSecondTimeCardiacDetectionActivity.class);
//                                                   intent.putExtra("devices_name",beanList.get(postion).binding);
//                                                   mcontxt.startActivity(intent);
//                                               }else{
//                                                   String binding = beanList.get(postion).binding;
//                                                   if(binding == null || binding.equals("null")) return;
//                                                   Intent intent = new Intent(mcontxt, FirstChangeCardiacDetectionActivity.class); //FirstChangeCardiacDetectionActivity
//                                                   intent.putExtra("devices_name",binding);
//                                                   mcontxt.startActivity(intent);
//                                               }
                                                Intent intent = new Intent(mcontxt, FrstChangeSecondTimeCardiacDetectionActivity.class);
                                                intent.putExtra("devices_name",beanList.get(postion).binding);
                                                mcontxt.startActivity(intent);

                                            }
                                            @Override
                                            public void onItemFirstButtonClick(View v, int position) {

                                            }
                                        });

                                    }
                                }
                            }
                        });


                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                       // NLogger.e("TAG", "===main==22=======e=================" + e);
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });


    }


}
